﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Composite.C1Console.Security;
using Composite.Data;
using LCA.Common;

namespace LCA.Security
{
    /// <summary>
    /// A Wrapper class to 'UserValidationFacade' of C1 Console.
    /// </summary>
    public class LCAAuthentication
    {
        public static string LoginUrl = "/Login";
        public static string AdminUrl = "/Composite/top.aspx";
        public static Guid HomePageId = Guid.Parse("A3055286-0E90-4B04-99DD-FB1A61DDE0BF");

        private static List<Guid> PagesNeedLogin = new List<Guid>()
        {
            // System
            Guid.Parse("23DAF9D9-49FF-4FDC-AFD5-AB0CE58C663A"),
            // Flow
            Guid.Parse("0C5C0880-C54D-48B9-9374-F0B8ED5E4230")
        };

        public LCAAuthentication()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public static bool IsLoggedIn()
        {
            return UserValidationFacade.IsLoggedIn();
        }


        public static void Logout()
        {
            UserValidationFacade.Logout();
        }

        public static string GetUsername()
        {
            return UserValidationFacade.GetUsername();
        }


        public static bool Authenticate(string username, string password)
        {
            return UserValidationFacade.FormValidateUser(username, password);

        }

        public static string GetAfterLoginUrl()
        {
            string result = "";
            

            return result;

        }
        
        public static void PerformPermissionCheck()
        {
            Guid pageId = SitemapNavigator.CurrentPageId;
            if (PagesNeedLogin.Contains(pageId))
            {
                if (!IsLoggedIn())
                {
                    HttpContext.Current.Response.Redirect(LCAAuthentication.LoginUrl);
                    HttpContext.Current.Response.StatusCode = 302;
                    HttpContext.Current.Response.End();
                }
            }
        }

        public static bool IsRole(UserGroup role)
        {
            
            using (DataConnection conn = new DataConnection())
            {
                //var serializedEntityToken = Request["EntityToken"];
                // var list = PermissionsFacade.GetPermissionsForCurrentUser(currentPage.GetDataEntityToken());
                string name = LCAAuthentication.GetUsername();
                var list = (from u in conn.Get<Composite.Data.Types.IUser>()
                           join rel in conn.Get<Composite.Data.Types.IUserUserGroupRelation>() on u.Id equals rel.UserId
                           join g in conn.Get<Composite.Data.Types.IUserGroup>() on rel.UserGroupId equals g.Id
                           where u.Username == name
                           select new 
                           {
                               Name = g.Name
                           }).ToList();


                bool isViewer = list.Any(x => x.Name == role.ToString());

                return isViewer;
            }

        }
    }
}